package com.ohuang.test_compose.page

import android.annotation.SuppressLint
import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import coil.annotation.ExperimentalCoilApi
import coil.compose.rememberImagePainter
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.SwipeRefreshState
import com.ohuang.test_compose.ContextUtil
import com.ohuang.test_compose.MainActivity2
import com.ohuang.test_compose.R
import com.ohuang.test_compose.bean.Action
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@ExperimentalCoilApi
@Composable
fun ListItem(name: String?) {

    var nameSate by remember {
        mutableStateOf("name")
    }
    Row() {

        Box(contentAlignment = Alignment.BottomCenter) {
            Image(
                bitmap = ImageBitmap.imageResource(id = R.drawable.icon_hj),
                null,
                modifier = Modifier.padding(10.dp),
                alignment = Alignment.Center
            )
            Text(text = "android", color = MaterialTheme.colors.primary)
        }
        val imageurl = "https://pics7.baidu.com/feed/8" +
                "b13632762d0f7036ab65a92bc48d5342797c5b1.jpeg?token" +
                "=7f317bb459035dfab63e3a4e28e50eff"
        Column(modifier = Modifier
            .padding(start = 10.dp)
            .clickable {
                val intent = Intent()
                intent.putExtra("1", name)
                ContextUtil.startActivity(intent, MainActivity2::class.java)
                nameSate = "$name is onclick"
            }
            .padding(start = 10.dp)) {
            Text(text = "Hello $name!", color = MaterialTheme.colors.primary)
            Text(text = "Hello ${nameSate}!", color = MaterialTheme.colors.primary)
            Image(
                painter = rememberImagePainter(data = imageurl),
                null, modifier = Modifier
                    .padding(10.dp)
                    .width(200.dp)
                    .height(100.dp),
                alignment = Alignment.Center
            )
        }
    }
}

@ExperimentalUnitApi
@Composable
fun textList( listSate: LazyListState = rememberLazyListState(),
              data: List<Action>,callback: (Action)->Unit ={}){
    LazyColumn(modifier = Modifier.fillMaxWidth(), state = listSate) {
        for (i in data) {
            item {
                Spacer(modifier = Modifier.height(10.dp))
                Text(text = i.name, modifier = Modifier.fillMaxWidth().height(50.dp)
                    .clickable {
                        callback(i)
                    }, fontSize = TextUnit(20F, TextUnitType.Sp))
                Spacer(modifier = Modifier.height(10.dp))
            }
        }
    }
}


@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun testList(
    listSate: LazyListState = rememberLazyListState(),
    data: List<String>,
    refreshCallbacks: () -> Unit
) {

    Surface(color = MaterialTheme.colors.background) {


        var refreshState by remember {
            mutableStateOf(SwipeRefreshState(false))
        }
        SwipeRefresh(state = refreshState, onRefresh = {
            refreshState = SwipeRefreshState(true)
            GlobalScope.launch {
                refreshCallbacks()
                refreshState = SwipeRefreshState(false)
            }
        }) {
            LazyColumn(modifier = Modifier.fillMaxWidth(), state = listSate) {
                for (i in data) {
                    item {
                        ListItem(i)
                    }
                }
            }
        }
    }
}